package hutoolandguava.tree;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;

import java.util.List;

/**
 * @author ZH
 */
public class TreeTest {
    public static void main(String[] args) {
        List<TreeNode> nodeList = CollUtil.newArrayList();
        nodeList.add(new TreeNode(1, 0, "系统管理", 5));
        nodeList.add(new TreeNode(11, 1, "用户管理", 222222));
        nodeList.add(new TreeNode(111, 11, "用户添加", 0));
        nodeList.add(new TreeNode(2, 0, "店铺管理", 1));
        nodeList.add(new TreeNode(21, 2, "商品管理", 44));
        nodeList.add(new TreeNode(221, 21, "商品管理2", 2));
        // 2.配置
        TreeNodeConfig config = new TreeNodeConfig();
        //config.setIdKey("id");//默认为id可以不设置
        config.setParentIdKey("pid");//默认为parentId可以不设置
        config.setDeep(4);//最大递归深度
        config.setWeightKey("weight");//排序字段

        // 3.转树，Tree<>里面泛型为id的类型
        List<Tree<Integer>> build = TreeUtil.build(nodeList, 0, config, (object, tree) -> {
            // 也可以使用 tree.setId(object.getId());等一些默认值
            tree.putExtra("id", object.getId());
            tree.putExtra("pid", object.getPid());
            tree.putExtra("name", object.getName());
            tree.putExtra("weight",object.getWeight());
        });

        System.out.println(build);
    }
}
